package com.yjhb.android.sbz.activity;

import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.adapter.RedPackDetailAdapter;
import com.yjhb.android.sbz.entity.RedPackDetails;
import com.yjhb.android.sbz.entity.RedPackItem;
import com.yjhb.android.sbz.mvp.redpackdetail.RedPackDetailPresenter;
import com.jojo.android.zxlib.base.mvp.CommonView;
import com.jojo.android.zxlib.tool.ZxLogTool;
import com.jojo.android.zxlib.tool.ZxSpanTool;
import com.jojo.android.zxlib.tool.ZxToast;
import com.jojo.android.zxlib.view.CircleImageView;
import com.jojo.android.zxlib.view.EmptyRecyclerView;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者 zx
 * 创建时间 2021/1/14 16:29
 */
public class RedPackDetailActivity extends BaseActivity<RedPackDetailPresenter> implements CommonView<List<RedPackItem>> {
    private final String TAG = "RedPackDetailActivity";

    RedPackDetails redPackDetails;
    @BindView(R.id.head_iv)
    CircleImageView headIv;
    @BindView(R.id.phone_tv)
    TextView phoneTv;
    @BindView(R.id.money_tv)
    TextView moneyTv;
    @BindView(R.id.empty_tv)
    TextView emptyTv;
    @BindView(R.id.received_tv)
    TextView received_tv;
    @BindView(R.id.emptyRecyclerView)
    EmptyRecyclerView emptyRecyclerView;

    RedPackDetailAdapter redPackDetailAdapter;

    List<RedPackItem> data;

    @Override
    protected RedPackDetailPresenter initPresenter() {
        return new RedPackDetailPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_red_pack_detail;
    }

    @Override
    protected void initUI() {
        redPackDetails = (RedPackDetails) getIntent().getSerializableExtra("redPackDetails");
        ZxLogTool.i(redPackDetails);
        emptyRecyclerView.setEmptyView(emptyTv);
        emptyRecyclerView.setAdapter(redPackDetailAdapter = new RedPackDetailAdapter(this));
    }

    @Override
    protected void initData() {
        presenter.listPullHistory(redPackDetails.getTaskId());
    }

    @Override
    public void refresh() {
        super.refresh();
        if (redPackDetails != null) {
            Glide.with(this).load(redPackDetails.getAvatar()).into(headIv);
            phoneTv.setText(redPackDetails.getNickName());
            String money = redPackDetails.getTotalMoney().setScale(2).toString();
            CharSequence charSequence = ZxSpanTool.ZoomSpan(money + "元", money.length(), money.length() + 1, 0.3f);
            moneyTv.setText(charSequence);
            if (data == null)
                received_tv.setText("已领取" + "0/" + redPackDetails.getMaxStock() + "个");
            else
                received_tv.setText("已领取" + data.size() + "/" + redPackDetails.getMaxStock() + "个");
        }
    }

    @Override
    protected void regEvent() {

    }

    @OnClick(R.id.back)
    void back() {
        finish();
    }

    @Override
    public void onWait() {
        loadingDialog.show();
    }

    @Override
    public void onSuccess(List<RedPackItem> data) {
        this.data = data;
        redPackDetailAdapter.setData(data);
        redPackDetailAdapter.notifyDataSetChanged();
        loadingDialog.cancel();
        received_tv.setText("已领取" + data.size() + "/" + redPackDetails.getMaxStock() + "个");
    }

    @Override
    public void onFailure(int code, String msg) {
        loadingDialog.cancel();
        ZxToast.showINFOBottomToast(msg);
    }
}
